package com.hz.service.impl;

import com.google.common.collect.Maps;
import com.hz.service.PrintService;
import com.hz.service.TestService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * <p>description</p >
 *
 * @author slj
 * @version 1.0
 * @date 2022/07/26 14:00
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PrintServiceImpl implements PrintService, ApplicationContextAware {



    private ApplicationContext applicationContext;


    private Map<Integer, TestService> serviceMap = Maps.newHashMap();


    @Override
    public int print(int i) {

        TestService testService = serviceMap.get(i);
        System.out.println(testService.test());
        return testService.test();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @PostConstruct
    public void initSettlementServiceMap() {
        Map<String, TestService> mp = applicationContext.getBeansOfType(TestService.class);
        mp.forEach((k, service) -> {
            serviceMap.put(service.getType(), service);
        });
    }

}
